package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.AuthorityDistributeRvAdapter;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthorityWrapItemBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.List;


/**
 * @author : mundane
 * @time : 2017/9/20 10:19
 * @description :
 * @file : BRCFDTagsListFragment.java
 */

public class AuthorityDistributeListFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private List<AuthorityWrapItemBean> mDataList;
    private AuthorityDistributeRvAdapter mRvAdapter;
    private EasyPopupWindow mEasyPopupWindow;
    private View mFlCard;

    public interface OnButtonClickedListener {
        void onButtonClicked();
    }

    private OnButtonClickedListener mButtonClickedListener;

    public void setOnButtonClickedListener(OnButtonClickedListener onButtonClickedListener) {
        mButtonClickedListener = onButtonClickedListener;
    }

    public static AuthorityDistributeListFragment newInstance(ArrayList<AuthorityWrapItemBean> list) {
        AuthorityDistributeListFragment fragment = new AuthorityDistributeListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(UserCenterConstant.KEY_AUTHORITY_ITEM_BEAN, list);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDataList = bundle.getParcelableArrayList(UserCenterConstant.KEY_AUTHORITY_ITEM_BEAN);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_distribute_authority, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        // findViewById
        mRecyclerView = $(R.id.rv);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mRvAdapter = new AuthorityDistributeRvAdapter(mDataList, mRecyclerView.getHeight());
//		mRecyclerView.addItemDecoration(new AuthorityDistributeRvItemDecoration());
                mRvAdapter.setOnItemClickListener(new AuthorityDistributeRvAdapter.OnItemClickListener() {
                    @Override
                    public void onButtonClicked(int position) {
                        AuthorityWrapItemBean authorityWrapItemBean = mDataList.get(position);
                        if (authorityWrapItemBean.isSelected) {
                            authorityWrapItemBean.isSelected = false;
                        } else {
                            authorityWrapItemBean.isSelected = true;
                        }
                        if (mButtonClickedListener != null) {
                            mButtonClickedListener.onButtonClicked();
                        }
                        mRvAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onTimeLabelClicked(View timeLableView, int remainder, int position) {
                        View rootView;
                        switch (remainder) {
                            case 0:
                                rootView = View.inflate(getContext(), R.layout.pop_up_authority_distribute_left, null);
                                createEasyPopupWindow(rootView, mDataList.get(position));
                                mEasyPopupWindow.showAsDropDown(timeLableView, -ConvertUtils.dip2px(getContext(), 16), 0);
                                break;
                            case 1:
                                rootView = View.inflate(getContext(), R.layout.pop_up_authority_distribute_middle, null);
                                createEasyPopupWindow(rootView, mDataList.get(position));
                                mEasyPopupWindow.showAsDropDown(timeLableView, -rootView.getMeasuredWidth() / 2 + ConvertUtils.dip2px(getContext(), 10), 0);
                                break;
                            case 2:
                                rootView = View.inflate(getContext(), R.layout.pop_up_authority_distribute_right, null);
                                createEasyPopupWindow(rootView, mDataList.get(position));
                                mEasyPopupWindow.showAsDropDown(timeLableView);
                                break;
                            default:
                                break;
                        }

                    }
                });
                mRecyclerView.setAdapter(mRvAdapter);
                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

    }

    private void createEasyPopupWindow(View rootView, AuthorityWrapItemBean authorityWrapItemBean) {
        TextView tvTime = (TextView) rootView.findViewById(R.id.tv);
        String cardName = authorityWrapItemBean.mItemBean.cardName;
        String validDate = authorityWrapItemBean.mItemBean.validDateStr;
        String invalidDate = authorityWrapItemBean.mItemBean.invalidDateStr;
        tvTime.setText(String.format("%s\n开始时间：%s\n结束时间：%s", cardName, validDate, invalidDate));
        mEasyPopupWindow = new EasyPopupWindow.Builder(getActivity())
                .setView(rootView)
                .setOutsideTouchHide(true)
                .setBackgroundAlpha(0.6f)
                .create();
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
